﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DotNET4Study
{
    public static class IGroupingStudy
    {
        public static void Execute()
        {
            //Demo1();

            //Demo2();

            Demo3();
        }

        


        private static void Demo1()
        {
            IGrouping<System.Reflection.MemberTypes, System.Reflection.MemberInfo> group =
                    typeof(String).GetMembers().GroupBy(member => member.MemberType).First();

            Console.WriteLine("\nValues that have the key '{0}':", group.Key);
            foreach (System.Reflection.MemberInfo mi in group)
                Console.WriteLine(mi.Name);
        }

        private static void Demo2()
        {
            //建立資料來源

            string[] weekDayList = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };

            //群組

            Console.WriteLine("===依第一個字母進行群組===");
            //寫法一： from...in...group...by 使用具名型別 IEnumerable<IGrouping<string, string>>
            Console.WriteLine("***寫法一***");
            IEnumerable<IGrouping<string, string>> groupQuery1 = from q in weekDayList
                                                                 group q by q.Substring(0, 1);
            foreach (var items in groupQuery1)
            {
                Console.WriteLine(string.Format("群組字母:{0}", items.Key));
                foreach (var item in items)
                {
                    Console.WriteLine(item);
                }
            }

            //寫法二： from...in...group...by 使用匿名型別 var
            Console.WriteLine("***寫法二***");
            var groupQuery2 = from q in weekDayList
                              group q by q.Substring(0, 1);
            foreach (var items in groupQuery2)
            {
                Console.WriteLine(string.Format("群組字母:{0}", items.Key));
                foreach (var item in items)
                {
                    Console.WriteLine(item);
                }
            }



            //寫法三：直接使用集合物件的方法GroupBy
            Console.WriteLine("***寫法三***");
            var groupQuery3 = weekDayList.GroupBy(q => q.Substring(0, 1));
            foreach (var items in groupQuery3)
            {
                Console.WriteLine(string.Format("群組字母:{0}", items.Key));
                foreach (var item in items)
                {
                    Console.WriteLine(item);
                }
            }
        }

        private static void Demo3()
        {
            List<Tuple<string, string>> list = new List<Tuple<string, string>>();
            list.Add(Tuple.Create<string, string>("qingdao", "shinan"));
            list.Add(Tuple.Create<string, string>("qingdao", "sifang"));
            list.Add(Tuple.Create<string, string>("jinan", "lixia"));

            var v= list.GroupBy(t=>t.Item1);
            foreach (var p in v)
            {
                Console.WriteLine("组名称为{0}",p.Key);
                foreach (var t in p)
                {
                    Console.WriteLine("\t值为{0}",t);
                }
                Console.WriteLine("---------------");
            }

        }
    }
}
